﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Android.Content;
using Android.OS;
using Android.Views;
using Android.Locations;
using Android.Widget;
using GBU.Services.Common;

namespace GBU.Services.LocationService
{
    public class GpsView:View,ILocationListener
    {
        public event EventHandler<LocationEventArgs> LocationChanged;

        private LocationManager locationManager;

        long locationInterval=1000; //gps 获取数据频率 单位MS
        private  int minDistance = 1;//gps 最小精度,单位米

        private string locationProvider;

        public GpsView(Context context) : base(context)
        {
            locationManager = Context.GetSystemService(Context.LocationService) as LocationManager;
        }

        public void StartLocationListen()
        {
            Criteria locationCriteria = new Criteria();
            locationCriteria.Accuracy = Accuracy.Fine;
            locationCriteria.PowerRequirement = Power.NoRequirement;

             locationProvider = locationManager.GetBestProvider(locationCriteria, true);
            if (!string.IsNullOrEmpty(locationProvider))
            {
                locationManager.RequestLocationUpdates(locationProvider, locationInterval, minDistance, this);
            }
            else
            {
                Toast.MakeText(this.Context, "GPS定位仪未打开,或您的设备不支持GPS定位仪", ToastLength.Short).Show();
            }
        }

        public LocationData GetInitLocation()
        {
            Location location = locationManager.GetLastKnownLocation(locationProvider);
            return new LocationData()
                {
                    Altitude = location.Altitude,
                    Latitude = location.Latitude,
                    Longitude = location.Longitude,
                    Time = location.Time
                };
        }

        public void StopLocationListen()
        {
            LocationChanged = null;
            locationManager.Dispose();
        }

        /// <summary>
        /// 设置最小更新位置频率(ms)
        /// </summary>
        /// <param name="locatinInterval"></param>
        public void SetLocationInterval(long interval)
        {
            locationInterval = interval;
        }

        /// <summary>
        /// 设置最小经度(两个点之间的距离)(米)
        /// </summary>
        /// <param name="meter"></param>
        public void SetMinimumDistance(int meter)
        {
            minDistance = meter;
        }

        public void OnLocationChanged(Location location)
        {
            var jingdu = location.Longitude; //经度
            var weidu = location.Latitude; //纬度
            var haiba = location.Altitude;//海拔
            var datetimelong = location.Time;//时间(卫星时间)
            
            if (LocationChanged != null)
            {
                LocationChanged(this,new LocationEventArgs(){ LocationData=new LocationData(){Altitude = haiba,Latitude = weidu,Longitude = jingdu,Time = datetimelong}});
            }
        }

        public void OnProviderDisabled(string provider)
        {
            
        }

        public void OnProviderEnabled(string provider)
        {
            
        }

        public void OnStatusChanged(string provider, Availability status, Bundle extras)
        {
            
        }
    }
}
